	// SearchGroup 选项组
	var SearchItem=function(template,source){
		this.template=template
		this.source=source;
	}
	SearchItem.prototype.init=function(){
		this.create();
	}
	SearchItem.prototype.template=""
	SearchItem.prototype.create=function(){
		var html=this.template
		this.selector=$(html);
		this.selector.get(0).className="searchItem";
		//console.log(this.selector);
		
		var itemRelation=this.source.itemRelation;
		var field=this.source.field;
		if(typeof field=="undefined" || field==""){
			field=$("select[name='field']",this.selector).val(); 
			console.log($("select[name='field']",this.selector));
		}
		

		var relation=this.source.relation;
		var value=this.source.value;
		var self=this;
		
		$("input[name='itemRelation']",this.selector).val(itemRelation);
		$("select[name='field'] option[value='"+field+"']",this.selector).attr("selected", true); 
		$("select[name='relation'] option[value='"+relation+"']",this.selector).attr("selected", true); 
		$("input[name='value']",this.selector).val(this.source.value);
		
		var type=$("select[name='field'] option[value='"+field+"']",this.selector).attr("data-type");
		//console.log(11);
		//console.log(this.source);
		//console.log(field);
		//console.log(22);
		var sourceInputSelector=$("input[name='value']",self.selector);
		var inputSelector=self.getInputSelector(type);
		
		$(inputSelector).replaceAll(sourceInputSelector);
		$(inputSelector).val(this.source.value);
		
		console.log(111);
		
		$("select[name='field']",this.selector).bind("change",function(event){
			console.log(222);
			//console.log(event);
			var target=event.target;
			//console.log(target);
			var selectedIndex=target.selectedIndex
			//console.log(selectedIndex);
			
			var selectedOptions=target.selectedOptions
			//console.log(selectedOptions);
			
			var type=$(selectedOptions).attr("data-type");
			//console.log(type);
			
			var sourceInputSelector=$("input[name='value'],select[name='value']",self.selector);
			var inputSelector=self.getInputSelector(type);
			
			console.log(type);
			console.log(sourceInputSelector);
			console.log(inputSelector);
			
			$(inputSelector).replaceAll(sourceInputSelector);
			
			/*if(type=="text"){
				$("<input type='text' name='value' value=''>").replaceAll(sourceInputSelector);
			}
			if(type=="number"){
				$("<input type='number' name='value' value=''>").replaceAll(sourceInputSelector);
			}
			if(type=="date"){
				$("<input type='date' name='value' value=''>").replaceAll(sourceInputSelector);
			}
			if(type=="stateSelect"){
				$('<select name="value"><option value="1">草稿</option><option value="2">待审核</option><option value="3">已审核</option><option value="4">已关闭</option><option value="0">已删除</option></select>').replaceAll(sourceInputSelector);
			}*/
		})
		//$("select[name='field']",this.selector).change();
	}
	SearchItem.prototype.getInputSelector=function(type){
		var selector=$("<input type='text' name='value' value=''>");
		if(type=="text"){
			selector=$("<input type='text' name='value' value=''>");
		}
		if(type=="number"){
			selector=$("<input type='number' name='value' value=''>");
		}
		if(type=="date"){
			selector=$("<input type='date' name='value' value=''>");
		}
		if(type=="stateSelect"){
			selector=$('<select name="value"><option value="">全部</option><option value="1">草稿</option><option value="5">已拒绝</option><option value="6">已弃审</option><option value="9">待审核</option><option value="10">已审核</option><option value="20">已关闭</option><option value="0">已删除</option></select>');
		}
		return selector;
	}
	SearchItem.prototype.getElement=function(){
		return $(this.selector).get(0);
	}
	
	var SearchGroup=function(selector){
		this.selector=selector;
	}
	SearchGroup.prototype.init=function(){
		this.create();
	}
	SearchGroup.prototype.itemList=new Array();
	SearchGroup.prototype.create=function(){
		var sourceSearchItemList=$(".source .searchItem",this.selector)
		var sourceSearchItem=null;
		var sourceList=new Array();
		var source=null;
		
		var relation="";
		var field="";
		var source="";
		var value="";
		
		for(var i=0;i<sourceSearchItemList.length;i++){
			var sourceSearchItem=sourceSearchItemList[i]
			var source={
				itemRelation:$("input[name='itemRelation']",sourceSearchItem).val(),
				field:$("input[name='field']",sourceSearchItem).val(),
				relation:$("input[name='relation']",sourceSearchItem).val(),
				value:$("input[name='value']",sourceSearchItem).val()
			}
			sourceList.push(source);
		}
		this.searchItemTemplate=$(".searchItemTemplate",this.selector).eq(0).prop("outerHTML");
		$(".searchItemTemplate",this.selector).remove();
		$(".source",this.selector).remove();
		for(var i=0;i<sourceList.length;i++){
			source=sourceList[i]
			item=this.addItem(source.itemRelation,source);
		}
	}
	SearchGroup.prototype.addItem=function(type,source){
		var template=this.searchItemTemplate;
		var item=new SearchItem(this.searchItemTemplate,source);
		item.init();
		$(this.selector).append(item.getElement());
		if(type=="and"){
			$(item.getElement()).addClass("and");
			$(".removeButton",item.getElement()).addClass("and");
			$(".removeButton",item.getElement()).val("且");
			
			$("input[name='itemRelation']",item.getElement()).val("and");
		}
		if(type=="or"){
			$(item.getElement()).addClass("or");
			$(".removeButton",item.getElement()).addClass("or");
			$(".removeButton",item.getElement()).val("或");
			
			$("input[name='itemRelation']",item.getElement()).val("or");
		}
		
		var self=this;
		$(".addButton",item.getElement()).bind("click",function(event){
			var target=event.currentTarget;
			var value=target.value;
			
			
			var source={
				itemRelation:"",
				field:"",
				relation:"",
				value:""
			}
			if(value=="且"){
				self.addItem("and",source);
			}else{
				self.addItem("or",source);
			}
		});
		$(".removeButton",item.getElement()).bind("click",function(event){
			var target=event.currentTarget;
			self.removeItem(target);
		});
		this.update();
	}
	SearchGroup.prototype.removeItem=function(target){
		$(target).parent().remove();
		this.update();
	}
	SearchGroup.prototype.update=function(){
		var searchItemList=$(".searchItem",this.selector)
		var total=searchItemList.length;i
		var searchItem=null;
		for(var i=0;i<searchItemList.length;i++){
			searchItem=searchItemList[i]
			if(i==0){
				//console.log(1);
				$(searchItem).addClass("first");
			}
			if(i<total-1){
				$(searchItem).removeClass("last");
			}
			if(i==total-1){
				$(searchItem).addClass("last");
			}
		}
	}
	HTMLTurbo.control.SearchGroup=SearchGroup;